package com.hupu.jrs.ui.report

import android.content.Context
import android.content.Intent
import android.support.v7.widget.Toolbar
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.BaseAdapter
import android.widget.EditText
import android.widget.ImageView
import android.widget.ListAdapter
import android.widget.ListView
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import java.util.ArrayList
import javax.inject.Inject
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.hupu.jrs.R
import com.hupu.jrs.ui.BaseSwipeBackActivity

/**
 * Created by sll on 2016/3/11.
 */
class ReportActivity : BaseSwipeBackActivity(), ReportContract.View {

    @Inject
    internal var mPresenter: ReportPresenter? = null

    @BindView(R.id.toolbar)
    internal var toolbar: Toolbar? = null
    @BindView(R.id.lvTypes)
    internal var lvTypes: ListView? = null
    @BindView(R.id.etContent)
    internal var etContent: EditText? = null

    private val list = ArrayList<String>()
    private var adapter: ReportAdapter? = null
    private var type = 1
    private var mDialog: MaterialDialog? = null
    private var tid: String? = null
    private var pid: String? = null

    protected override val isApplyStatusBarTranslucency: Boolean
        get() = true

    protected override val isApplyStatusBarColor: Boolean
        get() = true

    override fun initContentView(): Int {
        return R.layout.activity_report
    }

    override fun initInjector() {
        DaggerReportComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule())
                .reportModule(ReportModule())
                .build()
                .inject(this)
    }

    override fun initUiAndListener() {
        ButterKnife.bind(this)
        mPresenter!!.attachView(this)
        initToolBar(toolbar)
        setTitle("举报")
        pid = getIntent().getStringExtra("pid")
        tid = getIntent().getStringExtra("tid")
        mDialog = MaterialDialog.Builder(this).title("提示").content("正在举报").progress(true, 0).build()
        initData()
        adapter = ReportAdapter()
        lvTypes!!.adapter = adapter
        setListViewHeightBasedOnChildren(lvTypes!!)

        lvTypes!!.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            adapter!!.notifyDataSetChanged()
            type = position + 1
        }
    }

    private fun initData() {
        list.add("\u5e7f\u544a\u6216\u5783\u573e\u5185\u5bb9")
        list.add("\u8272\u60c5\u66b4\u9732\u5185\u5bb9")
        list.add("\u653f\u6cbb\u654f\u611f\u8bdd\u9898")
        list.add("\u4eba\u8eab\u653b\u51fb\u7b49\u6076\u610f\u884c\u4e3a")
    }

    fun setListViewHeightBasedOnChildren(listView: ListView) {
        // 获取ListView对应的Adapter
        val listAdapter = listView.adapter ?: return

        var totalHeight = 0
        var i = 0
        val len = listAdapter.count
        while (i < len) {
            // listAdapter.getCount()返回数据项的数目
            val listItem = listAdapter.getView(i, null, listView)
            // 计算子项View 的宽高
            listItem.measure(0, 0)
            // 统计所有子项的总高度
            totalHeight += listItem.measuredHeight
            i++
        }

        val params = listView.layoutParams
        params.height = totalHeight + listView.dividerHeight * (listAdapter.count - 1)
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.layoutParams = params
    }

    override fun showLoading() {
        if (!mDialog!!.isShowing && !isFinishing()) {
            mDialog!!.show()
        }
    }

    override fun hideLoading() {
        if (mDialog!!.isShowing && !isFinishing()) {
            mDialog!!.dismiss()
        }
    }

    override fun reportSuccess() {

    }

    @OnClick(R.id.btCommit)
    internal fun btCommitClick() {
        mPresenter!!.submitReports(tid.toString(), pid.toString(), type.toString(), etContent!!.text.toString())
    }

    inner class ReportAdapter : BaseAdapter() {

        override fun getCount(): Int {
            return list.size
        }

        override fun getItem(position: Int): String {
            return list[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            var convertView = convertView
            val holder: ViewHolder
            if (convertView == null) {
                convertView = LayoutInflater.from(this@ReportActivity)
                        .inflate(R.layout.item_list_report, parent, false)
                holder = ViewHolder(convertView)
                convertView!!.tag = holder
            } else {
                holder = convertView.tag as ViewHolder
            }
            if (lvTypes!!.isItemChecked(position)) {
                holder.ivCheck!!.visibility = View.VISIBLE
            } else {
                holder.ivCheck!!.visibility = View.GONE
            }
            holder.tvType!!.text = getItem(position)
            return convertView
        }

        internal inner class ViewHolder(view: View) {
            @BindView(R.id.tvType)
            var tvType: TextView? = null
            @BindView(R.id.ivCheck)
            var ivCheck: ImageView? = null

            init {
                ButterKnife.bind(this, view)
            }
        }
    }

    protected override fun onDestroy() {
        super.onDestroy()
        mPresenter!!.detachView()
    }

    companion object {

        fun startActivity(mContext: Context, tid: String, pid: String) {
            val intent = Intent(mContext, ReportActivity::class.java)
            intent.putExtra("tid", tid)
            intent.putExtra("pid", pid)
            mContext.startActivity(intent)
        }
    }
}
